import { defineStore } from 'pinia'
import { store } from '/@/store'
import type { AxiosResponse } from 'axios'
import type { Res } from '/@/tool/Res'
import type { Param } from '/@/tool/Param'
import type { CoreTree } from '/@/tool/CoreTree'

import codingService from '/@/api/cn/core/coding/codingService'

import type { ProjectModule } from '/@/model/cn/core/coding/eo/ProjectModule'

import type { DbTablesInfoVO } from '/@/model/cn/core/coding/vo/DbTablesInfoVO'

import type { DbTableConf } from '/@/model/cn/core/coding/eo/DbTableConf'

interface CodingState {
}

export const useCodingStore = defineStore({
  id: 'coding',
  state: (): CodingState => ({
  }),
  getters: {
  },
  actions: {
    addDataSource(param: Param<ProjectModule>): Promise<Res<ProjectModule>> {
      return new Promise<Res<ProjectModule>>((resolve, reject) => {
        codingService.addDataSource(param).then((res: AxiosResponse<Res<ProjectModule>>) => {
          resolve(res.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    getDbAllTablesInfo(param: Param<ProjectModule>): Promise<Res<DbTablesInfoVO>> {
      return new Promise<Res<DbTablesInfoVO>>((resolve, reject) => {
        codingService.getDbAllTablesInfo(param).then((res: AxiosResponse<Res<DbTablesInfoVO>>) => {
          resolve(res.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    batchSaveOrUpdateDbTablesConf(param: Param<DbTableConf>): Promise<Res<DbTableConf>> {
      return new Promise<Res<DbTableConf>>((resolve, reject) => {
        codingService.batchSaveOrUpdateDbTablesConf(param).then((res: AxiosResponse<Res<DbTableConf>>) => {
          resolve(res.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    getProjectModuleDbTableConfTreeData(param: Param<ProjectModule>): Promise<Res<CoreTree<any>>> {
      return new Promise<Res<CoreTree<any>>>((resolve, reject) => {
        codingService.getProjectModuleDbTableConfTreeData(param).then((res: AxiosResponse<Res<CoreTree<any>>>) => {
          resolve(res.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    generateProjectModule(param: Param<ProjectModule>): Promise<Res<ProjectModule>> {
      return new Promise<Res<ProjectModule>>((resolve, reject) => {
        codingService.generateProjectModule(param).then((res: AxiosResponse<Res<ProjectModule>>) => {
          resolve(res.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    generate(param: Param<DbTableConf>): Promise<Res<DbTableConf>> {
      return new Promise<Res<DbTableConf>>((resolve, reject) => {
        codingService.generate(param).then((res: AxiosResponse<Res<DbTableConf>>) => {
          resolve(res.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    delProCodeFiles(param: Param<DbTableConf>): Promise<Res<DbTableConf>> {
      return new Promise<Res<DbTableConf>>((resolve, reject) => {
        codingService.delProCodeFiles(param).then((res: AxiosResponse<Res<DbTableConf>>) => {
          resolve(res.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    regenerate(param: Param<DbTableConf>): Promise<Res<DbTableConf>> {
      return new Promise<Res<DbTableConf>>((resolve, reject) => {
        codingService.regenerate(param).then((res: AxiosResponse<Res<DbTableConf>>) => {
          resolve(res.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    generateVue(param: Param<DbTableConf>): Promise<Res<DbTableConf>> {
      return new Promise<Res<DbTableConf>>((resolve, reject) => {
        codingService.generateVue(param).then((res: AxiosResponse<Res<DbTableConf>>) => {
          resolve(res.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    delVueCodeFiles(param: Param<DbTableConf>): Promise<Res<DbTableConf>> {
      return new Promise<Res<DbTableConf>>((resolve, reject) => {
        codingService.delVueCodeFiles(param).then((res: AxiosResponse<Res<DbTableConf>>) => {
          resolve(res.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    regenerateVue(param: Param<DbTableConf>): Promise<Res<DbTableConf>> {
      return new Promise<Res<DbTableConf>>((resolve, reject) => {
        codingService.regenerateVue(param).then((res: AxiosResponse<Res<DbTableConf>>) => {
          resolve(res.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
  },
})

export function useCodingStoreWithout() {
  return useCodingStore(store)
}
